
cap log using "Loanshark_Manuscript_Tables.log", replace

*******************************
*
* Produce Tables for ReStats Manuscript
*
*******************************

use loanshark_data_final, clear


**********************************************************************************************
******************************* Table 1: Borrower's Characteristics **************************
**********************************************************************************************

preserve
keep if id_tag == 1
*
************************* Panel A: Basic
*

tab gender 
tab ethnicity
tab edu
tab workstatus
tab incomemon

*
************************** Panel B: Borrowing Behavior & Spending
*

tab debt 
tab  borrowexp 

rename famfrdborrowfreq bvar1
rename borrowfreq bvar2
rename famfrdfailrepayfreq bvar3
rename failrepayfreq bvar4
rename drinking bvar5
rename drug bvar6
rename gambling bvar7
count
matrix mat0=J(1,4,r(N))
matrix mat1=J(7,4,0)
matrix mat2=J(7,4,0)

forval r=1(1)7{
	forval c=1(1)4{
	count if bvar`r'==`c'
	return list
	matrix mat1[`r',`c']=r(N)
	matrix mat2[`r',`c']=mat1[`r',`c']/mat0[1,`c']*100
	}
}
matrix matT1Borr=mat2
matrix rownames matT1Borr = Borrow_From_FamilyFriends Borrow_From_Loanshark Fail_to_Repay_FamFriend Fail_to_Repay_Loanshark Drinking Drug_Abuse Gambling
matrix colnames matT1Borr = Never Occasionally Regular Very_Frequently
matrix list matT1Borr
outtable using table_borrower, mat(matT1Borr) append caption("Borrow Behavior") f(%9.2f) nobox
rename bvar1 famfrdborrowfreq
rename bvar2 borrowfreq
rename bvar3 famfrdfailrepayfreq
rename bvar4 failrepayfreq
rename bvar5 drinking
rename bvar6 drug
rename bvar7 gambling




rename savepct bvar1
rename lavishpct bvar2
count
matrix mat0=J(1,4,r(N))
matrix mat1=J(2,4,0)
matrix mat2=J(2,4,0)

forval r=1(1)2{
	forval c=1(1)4{
	count if bvar`r'==`c'
	return list
	matrix mat1[`r',`c']=r(N)
	matrix mat2[`r',`c']=mat1[`r',`c']/mat0[1,`c']*100
	}
}
matrix matT1Spend=mat2
matrix rownames matT1Spend = Saving Lavish_Spending
*Col%
matrix colnames matT1Spend = Less_than_10\% 10\%_-_20\% 20\%_-_40\% More_than_40\% 
matrix list matT1Spend

outtable using table_borrower, mat(matT1Spend) replace caption("Spending") f(%9.2f) nobox
rename bvar1 savepct
rename bvar2 lavishpct

restore



*************************************************************************************
****************************** Table 2: Loan Characteristics ************************
*************************************************************************************

*
************************** Panel A
*

lab var repayendD "Finally Repay"

local vars = "loan loanasked rate_pct repaytime repayfull repayendD" 


	foreach v in `vars' {
			sum `v' 	
			sum `v' if enforcement == 0
			sum `v' if enforcement == 1
	}
	
count
tab enforcement
	

*
************************** Panel B Repayment 
*

*** How to repay
sum howrepayn1  howrepayn4 howrepayn2 howrepayn3 howrepayn6 howrepayn9 

tab conscription

*** Harassment

sum forcen11 forcen1  

* compute Humiliation (potential loss of face) 
sum forcen2 forcen3 forcen4  forcen6 forcen7  forcen16 forcen17 forcen18 forcen19
di .0194888+ .0011784+ .0031726+.0204859+.0229333 + .1725888 + .0034445+.0113307 + .0179478


sum forcen12 forcen0

* Compute vandalism
 sum forcen5 forcen13 forcen14 forcen15  forcen20
di .0590102+ .0073423 + .0658086 +.0300036+ .0002719 


*
************************** Panel C Condition
*

*** Borrow Condition
tab condit_a
sum conditn*
sum  conditn4  conditn1 conditn2 conditn5  conditn3 


*** Screening Docs
sum collateraln2 collateraln4 collateraln7 collateraln8

sum  collateraln3 collateraln6 
di .01513+.0058


*** Why this lender
sum whythisahlongn7 whythisahlongn2 whythisahlongn8 whythisahlongn9 whythisahlongn3  whythisahlongn1  whythisahlongn4


*** Why Borrow
sum reasonn5 reasonn4 reasonn14 reasonn9  reasonn6 reasonn12  reasonn10


	
*
************************** Panel D: Contract Dynamics ****************** 
*

***********************************************************
*********** Step A: Generate few more variables ***********
***********************************************************

******************* Create tags *******************
preserve
sort id sharkid5 enforcement year
egen pair5_year_tag = tag(id sharkid5 year)

** At borrower-sharkid5 pair level: max number of times transacted with a lender
bys id sharkid5 (year month): gen pair5_num     = _n 

******** END: Following are newly added for AER-Insight ********

gen never_contacted = (borrowhist == 0)
lab var never_contacted "Never contracted with this lender"

gen contract_num_year = .
replace contract_num_year = pair5_num if pair5_year_tag
lab var contract_num_year "Times contract with this lender per year"


gen rate_never_contacted =.
replace rate_never_contacted = rate*100 if borrowhist == 0
lab var rate_never_contacted "Nominal rate (if never contracted)"


local vars4 "never_contacted contract_num_year rate_never_contacted"

foreach v4 in `vars4' {
			sum `v4' 
			local `v4'_mean: disp %9.2f r(mean)
			local `v4'_sd: disp %9.2f r(sd)
	}
	
restore
***************************************************************************
****************************** Table 3: Regression ************************
***************************************************************************


preserve

sum rate_pct loan loanasked lendrate totalpaid latefee_share default howrepayn3 howrepayn2 lenderrequestwork conscription  noforce remind repayendD if enforcement==0

sum rate_pct loan loanasked lendrate totalpaid latefee_share default howrepayn3 howrepayn2 lenderrequestwork conscription  noforce remind repayendD if enforcement==1


gen coef = .
gen fake = 0
gen ydum = .
replace ydum = 0 if _n==1
gen enforced = 0 + 1*(_n>5) if _n<9
gen trend = _n


foreach v in rate_pct loan loanasked lendrate totalpaid latefee_share default howrepayn3 howrepayn2 lenderrequestwork conscription repayendD{
local rate_pctlbl "Nominal Rate"
local loanlbl "Loan"
local loanaskedlbl "Loan Asked"
local lendratelbl "Percent of Loan Asked Lent"
local totalpaidlbl "Total Paid"
local defaultlbl "Default"
local howrepayn2lbl "Repay by Borrow from Friends"
local howrepayn3lbl "Repay by Borrow from Family"
local repayendlbl "Repaid Eventually"
local latefee_sharelbl "Late Fee Paid as a Share of Loan"
local conscriptionlbl "Request Accepted if Requested"
local lenderrequestworklbl "Lender Requested Work for Him"

qui xi: reg `v' i.year
	forvalues x = 10/16 {
		replace ydum = _b[_Iyear_20`x'] if _n==`x'-8
		}
di "*********  `v' ****************"

reg ydum enforced
est store ols
	
qui xi: areg `v' i.year, abs(id)
	forvalues x = 10/16 {
		quietly replace ydum = _b[_Iyear_20`x'] if _n==`x'-8
		}
reg ydum enforced
est store fe_b	

qui xi: areg `v' i.year, abs(sharkid5)
	forvalues x = 10/16 {
		quietly replace ydum = _b[_Iyear_20`x'] if _n==`x'-8
		}
reg ydum enforced
est store fe_l	

qui xi: areg `v' i.year, abs(shark5_bor)
	forvalues x = 10/16 {
		quietly replace ydum = _b[_Iyear_20`x'] if _n==`x'-8
		}
reg ydum enforced	
est store fe_bl	

lab var enforced "``v'lbl'"
esttab ols fe_b fe_l fe_bl using table_reg_restats.tex, append  ///
	mtitle("OLS" "FE (Borrower)" "FE (Lender)" "FE (Borr-Lender)") noconstant ///
	label booktabs b(3) se(3) eqlabels(none)  unstack  noobs nostar ///
	fragment nolines nonumbers
	
}
restore




preserve
gen coef = .
gen fake = 0
gen ydum = .
replace ydum = 0 if _n==1
gen enforced = 0 + 1*(_n>5) if _n<9
gen trend = _n

keep if repayfull ==0 | repayend<2
	
foreach v in  noforce remind  {
local noforcelbl "No Harassment"
local remindlbl "Reminder Call "


qui xi: reg `v' i.year
	forvalues x = 10/16 {
		replace ydum = _b[_Iyear_20`x'] if _n==`x'-8
		}
reg ydum enforced
est store ols
	
qui xi: areg `v' i.year, abs(id)
	forvalues x = 10/16 {
		quietly replace ydum = _b[_Iyear_20`x'] if _n==`x'-8
		}
reg ydum enforced
est store fe_b	

qui xi: areg `v' i.year, abs(sharkid5)
	forvalues x = 10/16 {
		quietly replace ydum = _b[_Iyear_20`x'] if _n==`x'-8
		}
reg ydum enforced
est store fe_l	

qui xi: areg `v' i.year, abs(shark5_bor)
	forvalues x = 10/16 {
		quietly replace ydum = _b[_Iyear_20`x'] if _n==`x'-8
		}
reg ydum enforced	
est store fe_bl	

lab var enforced "``v'lbl'"
esttab ols fe_b fe_l fe_bl using table_reg_restats.tex, append  ///
	 nomtitles noconstant ///
	label booktabs b(3) se(3) eqlabels(none)  unstack  noobs nostar ///
    fragment nolines nonumbers
}

restore

log close
	
	
	
	
